// 公共js文件 

// $.init({
// 	swipeBack:true //启用右滑关闭功能
// });


//设置全局beforeSend
mui.ajaxSettings.beforeSend = function(xhr, setting) {
	//beforeSend演示,也可在$.ajax({beforeSend:function(){}})中设置单个Ajax的beforeSend
	xhr.dataType = "json"; // 返回值类型
	if(setting.async == false){

	}else{
		xhr.timeout = "10000"; // 超时
	}

	xhr.setRequestHeader("token", window.localStorage.getItem("token")); // 请求头带上token
	
};

//设置全局complete
mui.ajaxSettings.complete = function(data,r,r) {
	if (data.response && JSON.parse(data.response).code === 401) { // 401, token失效
		clearLoginInfo();
		mui.toast("token失效,请重新登录",{ duration:'long', type:'div' });
		setTimeout(function(){
			plus.runtime.restart();
		},3000)
		
	}
}

//设置全局error
mui.ajaxSettings.error = function(data) {
	mui.toast("失去连接",{ duration:'long', type:'div' })
}


// 清除登录信息
function clearLoginInfo(){
	window.localStorage.removeItem("token");
	window.localStorage.removeItem("user");
}

//工具集合Tools
window.T = {};
// 使用示例
// location.href = http://localhost:8080/index.html?id=123
// T.p('id') --> 123;
var url = function(name) {
	var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);
	if(r!=null)return  unescape(r[2]); return null;
};
T.p = url;

// 默认打开窗口样式
function defOpenWindow(id,url,extras){
	var aniShow = mui.os.plus ? "slide-in-right" : "zoom-fade-out";
	mui.openWindow({
		url: url,
		id: id,
		styles: {
			popGesture: "close",
			statusbar: {
				background: "#f7f7f7"
			}
		},
		show: {
			aniShow: aniShow,
			duration: 300
		},
		extras:extras
	});
}